package com.sz.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.gcord.gcorddemo.R;
import com.sz.bean.RecordInfo;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/27.
 */
public class DailAdapter extends BaseAdapter {

    private List<RecordInfo> recordInfos;
    private Context context;
    private LayoutInflater layoutInflater;

    private long lastGroupId = 0;

    public DailAdapter(Context context, List<RecordInfo> recordInfos) {
        this.recordInfos = recordInfos;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return recordInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return recordInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RecordInfo recordInfo = recordInfos.get(position);
        long groupId = recordInfo.getGroupId();
        ComsumViewHolder holder = null;
        if (convertView == null) {
            //获得组件，实例化组件
            holder = new ComsumViewHolder();
            convertView = layoutInflater.inflate(R.layout.list_item_layout, null);
            holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            holder.userNameTv = (TextView) convertView.findViewById(R.id.user_name_tv);
            holder.phoneNumTv = (TextView) convertView.findViewById(R.id.phone_num_tv);
            holder.dailStatusTv = (TextView) convertView.findViewById(R.id.dail_status_tv);
            holder.groupNameTv = (TextView) convertView.findViewById(R.id.group_name_tv);
            convertView.setTag(holder);
        } else {
            holder = (ComsumViewHolder) convertView.getTag();
        }
        if (lastGroupId != groupId) {//说明是新的一组，需要显示groupName
            holder.groupNameTv.setVisibility(View.VISIBLE);
            holder.layout.setVisibility(View.GONE);
            holder.groupNameTv.setText(recordInfo.getGroupName());
            lastGroupId=groupId;
        } else {
            holder.groupNameTv.setVisibility(View.GONE);
            holder.layout.setVisibility(View.VISIBLE);
            holder.userNameTv.setText(recordInfo.getUserName());
            holder.phoneNumTv.setText(recordInfo.getPhoneNum());
            int dailStatus = recordInfo.getDailStatus();
            if (dailStatus == 1)
                holder.dailStatusTv.setText("已接通");
            else if (dailStatus == 2)
                holder.dailStatusTv.setText("未接通");
        }

        return convertView;
    }

    class ComsumViewHolder {
        LinearLayout layout;
        TextView userNameTv;
        TextView phoneNumTv;
        TextView lastDailTimeTv;
        TextView dailStatusTv;
        TextView holdTimeTv;
        TextView groupNameTv;
    }
}
